<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * 贸易模式管理
 * 2016.10.21
 */

namespace Qwadmin\Controller;


class TradeController extends ComController{
    
    public function index()
    {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $m = M('trade');
        $pagesize = 10; #每页数量
        $offset = $pagesize * ($p - 1); //计算记录偏移量
        $count = $m->where($where)->count();
        $list = $m
                ->join('as t LEFT JOIN zt_customer as c ON t.site_id = c.id')
                ->where($where)->limit($offset . ',' . $pagesize)
                ->field('t.*,c.shop_name')
                ->select();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }
    
    
     
    /*
  * 添加贸易模式
  * */
    public function add()
    {
        $customer = M('customer')->select();
        $this->assign('customer',$customer);
        $this->display('form');
    }

    /*
    * 删除贸易模式
    * */
    public function del()
    {

        $ids = isset($_REQUEST['ids']) ? $_REQUEST['ids'] : false;

        if (is_array($ids)) {
            foreach ($ids as $k => $v) {
                $ids[$k] = intval($v);
            }
        }elseif(!$ids){
            $this->error('参数错误！');
            }

        $map['obj_ident'] = array('in', $ids);
        if (M('trade')->where($map)->delete()) {
            addlog('删除贸易模式ID：' . $ids);
            $this->success('恭喜，贸易模式删除成功！');
        } else {
            $this->error('参数错误！');
        }
    }

  
    /*
    * 修改贸易模式
    * */  
    public function edit($id = 0)
    {
        $obj_ident = I('obj_ident');
        $m = M('trade');
        $currentmenu = $m->where(array('obj_ident'=>$obj_ident))->find();
        if (!$currentmenu) {
            $this->error('参数错误！');
        }
        $customer = M('customer')->select();
        $this->assign('customer',$customer);
        $this->assign('currentmenu', $currentmenu);
        $this->display('form');
    }

    public function update()
    {
        $m = M('trade');
        $obj_ident = I('post.obj_ident');
        $data['name'] = I('post.name', '', 'strip_tags');
        $data['code'] = strtoupper(I('post.code'));
        $data['site_id'] = I('post.site_id');
        $data['trade_id'] = I('post.trade_id');
       
        if ($obj_ident) {
            $flag = M('trade')->where(array('name'=> $data['name'],'site_id'=>$data['site_id'],"obj_ident"=>array('neq',$obj_ident)))->find();
            if($flag){
                $this->error('该贸易模式已经存在！');
            }
            $data['obj_ident'] =  $data['code'].'_'. $data['site_id'];
            M('trade')->where(array('obj_ident'=>$obj_ident))->delete();
            M('trade')->data($data)->add();
            addlog('编辑贸易模式，ID：' . $obj_ident);
        } else {
            $flag=M('trade')->where(array('code'=> $data['code'],'site_id'=>$data['site_id']))->find();
                if($flag){
                    $this->error('该贸易模式已经存在！');
                }
                $data['obj_ident'] =  $data['code'].'_'. $data['site_id'];
             
                M('trade')->data($data)->add();
                addlog('新增贸易模式，名称：' . $data['obj_ident']);
        }

        $this->success('操作成功！',U('index'));
    }
}
